package com.ghsc.ghxc.jsb.ui.live.view.cust

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import com.cn.appcore.event.RxBus
import com.cn.appcore.event.RxBusBean
import com.cn.appcore.ext.loge
import com.cn.appcore.ext.setSingleClickListener
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustSignKey
import com.ghsc.ghxc.jsb.config.UserManager
import com.ghsc.ghxc.jsb.databinding.ImCustRedViewBinding
import com.netease.nimlib.sdk.msg.constant.MsgDirectionEnum
import com.netease.yunxin.kit.chatkit.ui.databinding.ChatBaseMessageViewHolderBinding
import com.netease.yunxin.kit.chatkit.ui.model.ChatMessageBean
import com.netease.yunxin.kit.chatkit.ui.normal.view.message.viewholder.NormalChatBaseMessageViewHolder

/**
 * 项目：GuoHuaMall
 * 包名：com.ghsc.ghxc.ui.message.yunxin.view
 * 类名：CusRedAttachmentViewHolder
 * 创建时间：2023/10/24 21:10
 * 创建人 @author： xiaolin
 * 描述 ：Todo...自定义消息发红包的view
 */
class CusRedAttachmentViewHolder(parent: ChatBaseMessageViewHolderBinding, viewType: Int) : NormalChatBaseMessageViewHolder(
    parent, viewType
) {

    private var binding //创建自己的layout布局
            : ImCustRedViewBinding? = null
    override fun addViewToMessageContainer() {
        binding = ImCustRedViewBinding.inflate(
            LayoutInflater.from(parent.context), messageContainer, true
        )
    }


    val userId = UserManager.getUserId()


    //去掉背景
    override fun onMessageBackgroundConfig(messageBean: ChatMessageBean?) {
        baseViewBinding.messageContainer.setBackgroundResource(R.color.title_transfer)
    }


    override fun onMessageStatus(data: ChatMessageBean?) {
        loge("xxx===刷新吗。")
        val bean = data!!.messageData.message.attachment as CusRedAttachment
            ?: return
        if (data!!.messageData.message.direct== MsgDirectionEnum.Out){
            //右边，发送

            binding!!.llLeft.visibility=View.GONE
            binding!!.llLeftFalse.visibility=View.GONE

            //需要判断。是否领取
            if (data.messageData.message.localExtension!=null){
                loge("获取红包自定义右边show"+data.messageData.message.localExtension.toString())
                var map: HashMap<String, String> = data.messageData.message.localExtension as HashMap<String, String>
                if (!TextUtils.isEmpty(map[userId])){
                    binding!!.llRight.visibility=View.GONE
                    binding!!.llRightFalse.visibility=View.VISIBLE
                }else{
                    binding!!.llRight.visibility=View.VISIBLE
                    binding!!.llRightFalse.visibility=View.GONE
                }
            }else {
                //没有领取
                binding!!.llRight.visibility=View.VISIBLE
                binding!!.llRightFalse.visibility=View.GONE
            }
            binding!!.rightMemoTrue.text=bean.memo
            binding!!.rightMemoFalse.text=bean.memo

        }else{
            //左边  接受
            binding!!.llRight.visibility=View.GONE
            binding!!.llRightFalse.visibility=View.GONE

            //需要判断。是否领取
            if (data.messageData.message.localExtension!=null){
                loge("获取红包自定义右边show"+data.messageData.message.localExtension.toString())
                var map: HashMap<String, String> = data.messageData.message.localExtension as HashMap<String, String>
                if (!TextUtils.isEmpty(map[UserManager.getUserId()])){
                    binding!!.llLeft.visibility=View.GONE
                    binding!!.llLeftFalse.visibility=View.VISIBLE
                }else{
                    binding!!.llLeft.visibility=View.VISIBLE
                    binding!!.llLeftFalse.visibility=View.GONE
                }
            }else {
                //没有领取
                binding!!.llLeft.visibility=View.VISIBLE
                binding!!.llLeftFalse.visibility=View.GONE
            }



            binding!!.leftMemoTrue.text=bean.memo
            binding!!.leftMemoFalse.text=bean.memo
        }

    }

    override fun bindData(message: ChatMessageBean?, lastMessage: ChatMessageBean?) {
        super.bindData(message, lastMessage)
        currentMessage = message
        val bean = message!!.messageData.message.attachment as CusRedAttachment
            ?: return
        if (message!!.messageData.message.direct== MsgDirectionEnum.Out){
            //右边，发送

            binding!!.llLeft.visibility=View.GONE
            binding!!.llLeftFalse.visibility=View.GONE

            //需要判断。是否领取
            if (currentMessage.messageData.message.localExtension!=null){
                loge("获取红包自定义右边show"+currentMessage.messageData.message.localExtension.toString())
                var map: HashMap<String, String> = currentMessage.messageData.message.localExtension as HashMap<String, String>
                if (!TextUtils.isEmpty(map[UserManager.getUserId()])){
                    binding!!.llRight.visibility=View.GONE
                    binding!!.llRightFalse.visibility=View.VISIBLE
                }else{
                    binding!!.llRight.visibility=View.VISIBLE
                    binding!!.llRightFalse.visibility=View.GONE
                }
            }else {
                //没有领取
                binding!!.llRight.visibility=View.VISIBLE
                binding!!.llRightFalse.visibility=View.GONE
            }
            binding!!.rightMemoTrue.text=bean.memo
            binding!!.rightMemoFalse.text=bean.memo

        }else{
            //左边  接受
            binding!!.llRight.visibility=View.GONE
            binding!!.llRightFalse.visibility=View.GONE

            //需要判断。是否领取
            if (currentMessage.messageData.message.localExtension!=null){
                loge("获取红包自定义右边show"+currentMessage.messageData.message.localExtension.toString())
                var map: HashMap<String, String> = currentMessage.messageData.message.localExtension as HashMap<String, String>
                if (!TextUtils.isEmpty(map[UserManager.getUserId()])){
                    binding!!.llLeft.visibility=View.GONE
                    binding!!.llLeftFalse.visibility=View.VISIBLE
                }else{
                    binding!!.llLeft.visibility=View.VISIBLE
                    binding!!.llLeftFalse.visibility=View.GONE
                }
            }else {
                //没有领取
                binding!!.llLeft.visibility=View.VISIBLE
                binding!!.llLeftFalse.visibility=View.GONE
            }



            binding!!.leftMemoTrue.text=bean.memo
            binding!!.leftMemoFalse.text=bean.memo
        }



        binding!!.llItem.setSingleClickListener {
            bean.let {
                RxBus.getDefault().post(
                    RxBusBean(
                        CustSignKey.CHAT_RED_SEE_LEFT,
                        CustSignKey.CHAT_RED_SEE_LEFT, message
                    )
                )
            }
//            //需要判断。是否领取
//            if (currentMessage.messageData.message.localExtension!=null){
//                loge("获取红包自定义右边show"+currentMessage.messageData.message.localExtension.toString())
////                var map: HashMap<String, String> =currentMessage.messageData.message.localExtension as HashMap<String, String>
////                if (!TextUtils.isEmpty(map[UserManager.getUserId()])){
////                    var  bundle = Bundle()
////                    bundle.putString("packetid",bean.id.toString())
////                    startActivity(RedExtensionIndexActivity().javaClass,bundle)
////                }else{
////                    showRobRedView(custMsgBean)
////                }
//            }else {
//                //没有领取
//                showRobRedView(custMsgBean)
//            }
        }
//        binding!!.tvText.text=bean.custmoerText
//        Glide.with(parent.context)
//            .load(
//                StickerManager.getInstance()
//                    .getStickerUri(attachment.getCatalog(), attachment.getChartLet())
//            )
//            .apply(RequestOptions().diskCacheStrategy(DiskCacheStrategy.NONE))
//            .into(binding.thumbnail)
    }
//    /** 抢红包 **/
//    private fun showRobRedView(bean: CustMsgBean){
//        XPopup.Builder(this)
//            .popupAnimation(PopupAnimation.ScaleAlphaFromCenter  )
//            .customHostLifecycle(lifecycle)
//            .navigationBarColor(getCompatColor(R.color.bg_dark_color))
//            //这个属性必须加，不然GiftFragment出问题
//            .isViewMode(true)
//            .dismissOnTouchOutside(true)
//            .dismissOnBackPressed(true)
//            .asCustom(RobRedPopView(this,bean))
//            .show()
//    }
}